/*
 * AisleNav Android Application
 * AisleNavActivity.java
 * project.AisleNav
 * 
 * CS495
 * 
 * Team:
 * 		Kirby Green
 * 		Mark Hale
 * 		Adam Warnock
 * 
 * Time: 6:14:34 PM
 * Date: Dec 4, 2011
 * 
 */
package project.AisleNav;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import project.AisleNav.ListActivity.ListMenuActivity;
import project.AisleNav.SaveableSets.SaveableLists;
import project.AisleNav.StoreActivity.StoreMenuActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

// TODO: Auto-generated Javadoc
/**
 * AisleNav AisleNavActivy.java
 * 
 * Main menu activity of AisleNav
 * 
 * @author Kirby
 * @version 0.1
 */
public class AisleNavActivity extends Activity {
	/** Called when the activity is first created. */
	private static Button listButton;
	
	/** The store button. */
	private static Button storeButton;
	
	/** The Constant SerializedFile. */
	private final static String SerializedFile = "SerializedFile";
	
	/**
	 * The Class Caller.
	 */
	public class Caller {
		
		/**
		 * Instantiates a new caller.
		 */
		public Caller(){
			System.out.println("Caller constructor");
			}
		
		/**
		 * Save.
		 */
		public void save(){
			System.out.println("Caller saving...");
			saveState();
			System.out.println("Caller saving complete.");
		}
		
		/**
		 * Load.
		 */
		public void load(){
			System.out.println("Caller loading...");
			loadState();
			System.out.println("Caller loading complete.");
		}
	}
	
	/** The caller. */
	public static Caller caller;

	/** Class of saveable Lists. */	
	public static SaveableLists SharedLists = new SaveableLists();

	/**
	 * Called when the activity is first created.
	 *
	 * @param savedInstanceState the saved instance state
	 * @author Kirby Green
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		caller = new Caller();
		setContentView(R.layout.start_screen);
		
		listButton = (Button) findViewById(R.id.listButton);
		storeButton = (Button) findViewById(R.id.storeButton);
		
		if(listButton == null) {
			System.out.println("ListButton is null!");
			finish();
		}
		if(storeButton == null) {
			System.out.println("StoreButton is null!");
			finish();
		}
		//openState();

		listButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(AisleNavActivity.this, ListMenuActivity.class);
				startActivity(i);
			}
		});

		storeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(AisleNavActivity.this, StoreMenuActivity.class);
				startActivity(i);
			}
		});
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	public void onPause() {
		super.onPause();
        //saveState();
	}
	
	/**
	 * Save state.
	 */
	public void saveState() {
		try {
		FileOutputStream fos = openFileOutput(SerializedFile, Context.MODE_PRIVATE);
		fos.write(serializeObject(SharedLists));
		fos.close();
		} catch(FileNotFoundException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		} 

	}
	
	/**
	 * Load state.
	 */
	public void loadState() {
		System.out.println("In loadState");
		FileInputStream fis;// = new FileInputStream();
		
		try {
			fis = openFileInput(SerializedFile);
			byte[] buffer = new byte[8192];
			System.out.println("Reading Buffer...");
			fis.read(buffer);
			System.out.println("Done reading buffer.");
			System.out.println("Deserializing object...");
			SharedLists = (SaveableLists) deserializeObject(buffer);
			System.out.println("Done deserializing.");
			fis.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Exiting loadState");
		
		
	}
	
	/**
	 * Serialize object.
	 *
	 * @param o the o
	 * @return the byte[]
	 */
	private static byte[] serializeObject(Object o) { 
	    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
	 
	    try { 
	      ObjectOutput out = new ObjectOutputStream(bos);
	      out.writeObject(o);
	      out.close(); 
	 
	      // Get the bytes of the serialized object 
	      byte[] buf = bos.toByteArray(); 
	 
	      return buf; 
	    } catch(IOException ioe) { 
	      Log.e("serializeObject", "error", ioe); 
	 
	      return null; 
	    } 
	  }
	
	/**
	 * Deserialize object.
	 *
	 * @param b the b
	 * @return the object
	 */
	private static Object deserializeObject(byte[] b) { 
	    try { 
	      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b)); 
	      Object object = in.readObject(); 
	      in.close(); 
	 
	      return object; 
	    } catch(ClassNotFoundException cnfe) { 
	      Log.e("deserializeObject", "class not found error", cnfe); 
	 
	      return null; 
	    } catch(IOException ioe) { 
	      Log.e("deserializeObject", "io error", ioe); 
	 
	      return null; 
	    } 
	  }
}